//
//  DHDoctorTableViewCell.m
//  DHKuaiYi
//
//  Created by 刘鹏飞 on 2016/11/13.
//  Copyright © 2016年 30-陈辉华. All rights reserved.
//

#import "DHDoctorTableViewCell.h"
#import "XHHAdditions.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>

@interface DHDoctorTableViewCell ()

/*---医生照片---*/
@property (nonatomic,weak)UIImageView *iconView;
/*---医生姓名---*/
@property(nonatomic,weak)UILabel *nameLabel;
/*---医科---*/
@property (nonatomic,weak)UILabel *medicineLabel;
/*---所属医院---*/
@property (nonatomic,weak)UILabel *hospitalLabel;
/*---匹配度---*/
@property (nonatomic,weak)UILabel *accuracyLabel;
/*---手术次数---*/
@property (nonatomic,weak)UILabel *operationLabel;
/*---鲜花---*/
@property (nonatomic,weak)UILabel *flowsLabel;
/*---旗帜---*/
@property (nonatomic,weak)UILabel *flagsLabel;

@end

@implementation DHDoctorTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupSubViews];
}

#pragma mark - 重写构造方法

/**
 重写构造方法
 */
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //创建子控件
        [self setupSubViews];
    }
    return self;
}


#pragma mark - 创建子控件并布局子控件

/**
 重写属性setter方法给控件赋值
 */
-(void)setDoctor:(DHDoctorModel *)doctor {
    //记录属性
    _doctor = doctor;
    
    NSURL *URL = [NSURL URLWithString:doctor.doctor_portrait];
    [self.iconView sd_setImageWithURL:URL placeholderImage:[UIImage imageNamed:@"doctor_defaultphoto_male"]];
    
    self.nameLabel.text = doctor.doctor_name;
    self.hospitalLabel.text = doctor.doctor_hospital_name;
    self.accuracyLabel.text = [NSString stringWithFormat:@"匹配度 %@",doctor.accuracy];
    self.operationLabel.text = [NSString stringWithFormat:@"手术: %@",doctor.operation_count];
    self.flagsLabel.text = [NSString stringWithFormat:@"锦旗: %@",doctor.banner];
    self.flowsLabel.text = [NSString stringWithFormat:@"鲜花: %@",doctor.flower];
    
}


/**
 创建子控件
 */
-(void)setupSubViews {
    //***********控件创建************//
    
    /*----照片----*/
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"doctor_defaultphoto_male"]];
    self.iconView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:iconView];
    
    /*----姓名----*/
    UILabel *nameLabel = [UILabel XHH_labelWithText:@"隔壁老王" fontSize:15 color:[UIColor blackColor]];
    [self.contentView addSubview:nameLabel];
    
    /*----医科----*/
    UILabel *medicineLabel = [UILabel XHH_labelWithText:@"外科医生" fontSize:14 color:[UIColor lightGrayColor]];
    [self.contentView addSubview:medicineLabel];
    
    /*----所属医院----*/
    UILabel *hospitalLabel = [UILabel XHH_labelWithText:@"隔壁老王" fontSize:12 color:[UIColor lightGrayColor]];
    [self.contentView addSubview:hospitalLabel];
    
    /*----匹配度----*/
    UILabel *accuracyLabel = [UILabel XHH_labelWithText:@"隔壁老王" fontSize:12 color:[UIColor redColor]];
    [self.contentView addSubview:accuracyLabel];
    
    /*----手术次数----*/
    UIImageView *operationImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"doctortutor1"]];
    [self.contentView addSubview:operationImageView];
    
    UILabel *operationLabel = [UILabel XHH_labelWithText:@"隔壁老王" fontSize:12 color:[UIColor blueColor]];
    [self.contentView addSubview:operationLabel];
    
    /*----鲜花数----*/
    UIImageView *flowsImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flower"]];
    [self.contentView addSubview:flowsImageView];
    
    UILabel *flowsLabel = [UILabel XHH_labelWithText:@"隔壁老王" fontSize:12 color:[UIColor blueColor]];
    [self.contentView addSubview:flowsLabel];
    
    /*----旗帜数----*/
    UIImageView *flagsImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flags"]];
    [self.contentView addSubview:flagsImageView];
    
    UILabel *flagsLabel = [UILabel XHH_labelWithText:@"隔壁老王" fontSize:12 color:[UIColor blueColor]];
    [self.contentView addSubview:flagsLabel];
    
    /*----右边btn----*/
    UIButton *accessoryBtn = [[UIButton alloc] init];
    [accessoryBtn setImage:[UIImage imageNamed:@"position-right"] forState:UIControlStateNormal];
    [self.contentView addSubview:accessoryBtn];
    
    //***********自动布局************//
    
    NSInteger margin = 8;
    
    /*----医生图片布局----*/
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView).offset(margin);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    /*----医生姓名布局----*/
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).offset(margin);
        make.top.equalTo(self.contentView).offset(margin);
    }];
    
    /*----医科布局----*/
    [medicineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel.mas_right).offset(10);
        make.bottom.equalTo (nameLabel);
    }];
    
    /*----医院名字布局----*/
    [hospitalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(margin);
    }];
    
    /*----右边btn布局----*/
    [accessoryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self.contentView);
        make.width.mas_equalTo(44);
    }];
    
    /*----匹配度布局----*/
    [accuracyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(accessoryBtn.mas_left).offset(-margin);
        make.baseline.equalTo(medicineLabel);
    }];
    
    /*----手术布局----*/
    [operationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(hospitalLabel);
        make.top.equalTo(hospitalLabel.mas_bottom).offset(margin*0.5);
        make.size.mas_equalTo(CGSizeMake(12, 12));
    }];
    [operationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(operationImageView.mas_right).offset(margin*0.5);
        make.centerY.equalTo(operationImageView);
    }];
    
    /*----鲜花布局----*/
    [flowsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(operationLabel).offset(100);
        make.size.mas_equalTo(operationImageView);
        make.centerY.equalTo(operationLabel);
    }];
    
    [flowsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(flowsImageView.mas_right).offset(margin*0.5);
        make.centerY.equalTo(flowsImageView);
    }];
    
    /*----旗帜布局----*/
    [flagsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(accessoryBtn.mas_left).offset(-margin);
        make.centerY.equalTo(operationLabel);
    }];
    
    [flagsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(flagsLabel.mas_left).offset(-(margin*0.5));
        make.centerY.equalTo(flagsLabel);
        make.size.mas_equalTo(operationImageView);
    }];
    
    //***********属性指向************//
    self.iconView = iconView;
    self.nameLabel = nameLabel;
    self.medicineLabel = medicineLabel;
    self.accuracyLabel = accuracyLabel;
    self.hospitalLabel = hospitalLabel;
    self.operationLabel = operationLabel;
    self.flagsLabel = flagsLabel;
    self.flowsLabel = flowsLabel;
    
}

@end
